import React from 'react';
import {formatMessage} from './intl';
import PropTypes from 'prop-types';
import {Button} from 'antd';
import ExcelImportIntl from '../common/ExcelImportIntl';
import routes from './routes';

const basePath = fetch.basePath || '';

const description = [
    formatMessage({
        id: 'fileImport.step2.description1',
        defaultMessage: '红色字段为必填项；'
    }),
    formatMessage({
        id: 'fileImport.step2.description2',
        defaultMessage: '国家 + 商品编码 必须唯一；'
    })
];

class ImportPanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.state = {loading: false};
    }
    onChange = () => this.setState({loading: !this.state.loading});
    onReturn = () => this.props.history.push(routes.query.url());
    render() {
        return ([
            <div key="return" className="page-toolbar">
                <Button type="primary" onClick={this.onReturn} disabled={this.state.loading}>
                    {formatMessage({
                        id: 'fileImport.btm.return',
                        defaultMessage: '返回'
                    })}
                </Button>
            </div>,
            <ExcelImportIntl
                key="import"
                template={this.props.hsCodesImportTemp}
                templateName={formatMessage({
                    id: 'hsCode.excelImportIntl.templateName',
                    defaultMessage: '申报要素导入模板'
                })}
                action={`${basePath}/parts/api/v1/hsCodes/import`}
                description={description}
                beforeUpload={this.onChange}
                afterUpload={this.onChange}/>
        ]);
    }
}

ImportPanel.propTypes = {
    history: PropTypes.object,
    hsCodesImportTemp: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    hsCodesImportTemp
} from './actions';
const mapDispatchToProps = dispatch => ({
    hsCodesImportTemp: () => dispatch(hsCodesImportTemp())
});
export default connect(null, mapDispatchToProps)(ImportPanel);
